//
//  userDefault.m
//  SXH-Bridge
//
//  Created by sxh on 2020/9/9.
//  Copyright © 2020 yuxin. All rights reserved.
//

#import "BaseFunction.h"
#import "JSBUtil.h"
#import "NSObject+UIViewController.h"
#import "SXHWebViewController.h"
#import "SXHDownloader.h"
#import "macro.h"
#import "TipsHud.h"
#import "BaseTools.h"
#import "PhotoSelector.h"
#import "QRViewController.h"
#import "SXHLocationHandler.h"
#import "LocationConverter.h"
#import "SXHMapViewController.h"
#import "SXHBaseWebView.h"

@implementation BaseFunction

#pragma mark - 判断设备类型ios Android
- (NSString *)mobileType:(NSString *)key {
    NSMutableDictionary *resultDic = [self setDict:@"200" message:@"成功" data:@{@"mobileType":@"ios"}];
    return [JSBUtil objToJsonString:resultDic];
}

- (void)mobileType:(NSString *)key :(JSCallBack)completionHander {
    NSString *result = [self mobileType:key];
    completionHander(result,YES);
}

#pragma mark - 数据获取
//同步api 方法返回值不能为void 参数必须申明
- (NSString *)getItem:(NSString *)key {
    if (![key isKindOfClass:[NSString class]]) {
        NSMutableDictionary *resultDic = [self setDict:@"500" message:@"key值必须为字符串" data:@{}];
        return [JSBUtil objToJsonString:resultDic];
    }
    id object = [[NSUserDefaults standardUserDefaults] valueForKey:key];
    if (object == nil || [object isKindOfClass:[NSNull class]]) {
        object = @"";
    }
    NSMutableDictionary *resultDic = [self setDict:@"200" message:@"数据获取成功" data:@{key: object}];
    return [JSBUtil objToJsonString:resultDic];
}

// 异步获取
- (void)getItem:(NSString *)key :(JSCallBack)completionHander {
    NSString *result = [self getItem:key];
    completionHander(result,YES);
}

#pragma mark - 数据存储
- (NSString *)setItem:(id)obj {
    if(![obj isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *errDic = [self setDict:@"500" message:@"传入参数为对象,示例：{'需要存储的key':'需要存储的值或对象'}" data:@{}];
        return [JSBUtil objToJsonString:errDic];
    }

    NSArray *keys = ((NSDictionary *)obj).allKeys;
    for (NSString *key in keys) {
        id value = obj[key];
        [[NSUserDefaults standardUserDefaults] setValue:value forKey:key];
    }
    
    NSDictionary *errDic = [self setDict:@"200" message:@"数据存储成功" data:@{}];
    return [JSBUtil objToJsonString:errDic];
}
//
- (void)setItem:(id)obj :(JSCallBack)completionHandler {
    NSString *result = [self setItem:obj];
    completionHandler(result,YES);
}

- (void)onAjaxRequest:(id)request :(JSCallBack)completeHandle {
    NSLog(@"听说你拦截了所有的请求发到原生来？？？%@",request);
    NSDictionary *errDic = @{@"statusMessage":@"请求成功呢",@"statusCode":@(1),@"responseText":@"123",@"headers":@"返回头"};
    NSString *result = [JSBUtil objToJsonString:errDic];
    completeHandle(result,YES);
}

#pragma mark - 打电话
- (NSString *)callPhone:(NSString *)mobile {
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",mobile];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
    
    NSMutableDictionary *resultDic = [self setDict:@"200" message:@"呼叫成功" data:@{}];
    return [JSBUtil objToJsonString:resultDic];
}

- (void)callPhone:(NSString *)mobile :(JSCallBack)completeHandle {
    NSString *result = [self callPhone:mobile];
    completeHandle(result,YES);
}

#pragma mark - 获取设备信息
- (NSString *)getDeviceInfo:(NSString *)info {
    NSString *name = [[UIDevice currentDevice] name];
    NSString *systemName = [[UIDevice currentDevice] systemName];
    NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
    NSString *identifierForVendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSMutableDictionary *result = [self setDict:@"200" message:@"获取设备信息成功" data:@{
        @"systemVersion":systemVersion,//系统版本
        @"systemName":systemName,       //系统名称
        @"identifierForVendor":identifierForVendor, //设备唯一码
        @"name":name,   //当前设备名称
        @"ip": [BaseTools getDeviceIPAddresses], //ip地址
        @"mac": [BaseTools getMacAddress],    //mac地址
    }];
    return [JSBUtil objToJsonString:result];
}

- (void)getDeviceInfo:(NSString *)info :(JSCallBack)completeHandle {
    NSString *result = [self getDeviceInfo:info];
    completeHandle(result,YES);
}

#pragma mark - 获取推送的token
- (NSString *)getDeviceToken:(NSString *)info {
    NSString *deviceToken = [[NSUserDefaults standardUserDefaults] valueForKey:@"deviceToken"];
    NSMutableDictionary *result = [self setDict:@"200" message:@"获取设备deviceToken成功" data:@{
        @"deviceToken": deviceToken ? deviceToken : @""
    }];
    return [JSBUtil objToJsonString:result];
}

- (void)getDeviceToken:(NSString *)info :(JSCallBack)completeHandle {
    NSString *result = [self getDeviceToken:info];
    completeHandle(result,YES);
}

#pragma mark - 打开新的web页
- (NSString *)openFrame:(id)options {
    if(![options isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *errDic = [self setDict:@"500" message:@"传入参类型错误" data:@{}];
        return [JSBUtil objToJsonString:errDic];
    }
    NSString *url = ((NSDictionary *)options)[@"url"];
    NSString *isHiddenNavigate = ((NSDictionary *)options)[@"isHiddenNavigate"];
    NSString *title = ((NSDictionary *)options)[@"title"];
    NSDictionary *dict = ((NSDictionary *)options)[@"params"];
    
    if ([self.zlzk_viewController isKindOfClass:[SXHWebViewController class]]) {
        
        SXHWebViewController *next = [SXHWebViewController new];
        NSMutableDictionary *params = @{}.mutableCopy;
        [params addEntriesFromDictionary:((SXHWebViewController *)self.zlzk_viewController).params];
        [params addEntriesFromDictionary:dict];
        next.params = params;
        next.url = url;
        next.hidesBottomBarWhenPushed = YES;
        next.ishiddenNavigate = isHiddenNavigate;
        next.title = title;
        
        if (self.zlzk_viewController.navigationController) {

            [self.zlzk_viewController.navigationController pushViewController:next animated:YES];
        }else {
            
            next.modalPresentationStyle = UIModalPresentationFullScreen;
            [self.zlzk_viewController presentViewController:next animated:YES completion:nil];
        }
    }
    NSMutableDictionary *result = [self setDict:@"200" message:@"打开下的web页成功" data:@{}];
    return [JSBUtil objToJsonString:result];
}

- (void)openFrame:(id)options :(JSCallBack)completeHandle {
    NSString *result = [self openFrame:options];
    completeHandle(result,YES);
}

#pragma mark - 获取注入前端的参数
- (NSString *)getParams:(NSString *)params {
    if ([self.zlzk_viewController isKindOfClass:[SXHWebViewController class]]) {
        SXHWebViewController *next = (SXHWebViewController *)self.zlzk_viewController;
        NSMutableDictionary *resultDict = [self setDict:@"200" message:@"获取参数列表成功" data:next.params];
        return [JSBUtil objToJsonString:resultDict];
    }
    for (UIView *view in self.zlzk_viewController.view.subviews) {
        if ([view isKindOfClass:[SXHBaseWebView class]]) {
            SXHBaseWebView *web = (SXHBaseWebView *)view;
            NSMutableDictionary *resultDict = [self setDict:@"200" message:@"获取参数列表成功" data:web.params];
            return [JSBUtil objToJsonString:resultDict];
        }
    }
    return @"";
}

- (void)getParams:(NSString *)params :(JSCallBack)completeHandle {
    NSString *result = [self getParams:params];
    completeHandle(result,YES);
}

#pragma mark - 关闭当前页面
- (NSString *)closeView:(NSString *)params {
    if (self.zlzk_viewController.navigationController) {
        [self.zlzk_viewController.navigationController popViewControllerAnimated:YES];
    }else {
        [self.zlzk_viewController dismissViewControllerAnimated:YES completion:nil];
    }
    return @"";
}

- (void)closeView:(NSString *)params :(JSCallBack)completeHandle {
    NSString *result = [self closeView:params];
    completeHandle(result,YES);
}

#pragma mark - 获取状态栏高度
- (NSString *)getStatusHeight:(NSString *)key {
    NSMutableDictionary *result = [self setDict:@"200" message:@"获取原生元素高度成功" data:@{
        /*状态栏高度*/
        @"StatusBarHeight" : @(kStatusBarHeight),
        /*导航栏高度*/
        @"NavBarHeight" : @(kNavBarHeight),
        /*状态栏和导航栏总高度*/
        @"NavBarAndStatusBarHeight" : @(kNavBarAndStatusBarHeight),
        /*TabBar高度*/
        @"TabBarHeight" : @(kTabBarHeight),
        /*顶部安全区域远离高度*/
        @"TopBarSafeHeight" : @(kTopBarSafeHeight),
        /*底部安全区域远离高度*/
        @"BottomSafeHeight" : @(kBottomSafeHeight),
        /*iPhoneX的状态栏高度差值*/
        @"TopBarDifHeight" : @(kTopBarDifHeight),
        /*导航条和Tabbar总高度*/
        @"NavAndTabHeight" : @(kNavAndTabHeight),
    }];
    return [JSBUtil objToJsonString:result];
}

- (void)getStatusHeight:(NSString *)key :(JSCallBack)completeHandle {
    NSString *result = [self getStatusHeight:key];
    completeHandle(result,YES);
}

#pragma mark - 获取应用更新

- (void)getWebAppUpdate:(id)updateInfo :(JSCallBack)completeHandle {
//    {
//        appVersion,   // app版本号
//        webVersion,// 当前更新版本号
//        updateUrl,    // 更新地址
//        isTips,       // 是否弹框更新
//    }
    if(![updateInfo isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *errDic = [self setDict:@"500" message:@"传入参数类型错误" data:@{}];
        completeHandle([JSBUtil objToJsonString:errDic],YES);
    }
    NSString *appVersion = ((NSDictionary *)updateInfo)[@"appVersion"];
    NSString *webVersion = ((NSDictionary *)updateInfo)[@"webVersion"];
    NSString *updateUrl = ((NSDictionary *)updateInfo)[@"updateUrl"];
    NSString *isTips = ((NSDictionary *)updateInfo)[@"isTips"];
    if (![appVersion isEqualToString:AppVersion]) {
        NSMutableDictionary *errDic = [self setDict:@"500" message:@"与当前app版本号不符" data:@{}];
        completeHandle([JSBUtil objToJsonString:errDic],YES);
    }
    
    //弹框
    if (isTips) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:@"当前版本有更新,是否需要更新内容？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSMutableDictionary *result = [self setDict:@"500" message:@"用户取消了更新" data:@{}];
            completeHandle([JSBUtil objToJsonString:result],YES);
        }];
        [alert addAction:ok];
        [alert addAction:cancel];
        [self.zlzk_viewController presentViewController:alert animated:YES completion:nil];
    }
    
    [[TipsHud sharedInstance] showLoadingOnView:self.zlzk_viewController.view];
    
    [[SXHDownloader sharedInstace] requestFileUrl:updateUrl webVersion:webVersion complete:^(FileStatus status, float progress, NSString * _Nullable downloadPath, NSString * _Nullable unzipPath) {

        NSLog(@"当前下载是否成功：%ld,-----%f",(long)status, progress);
        if (status == UnZipStatusFailed) {//下载成功但更新失败
            [[TipsHud sharedInstance] hideTips];
            NSLog(@"%@-----%@",downloadPath, unzipPath);
            NSMutableDictionary *errDic = [self setDict:@"500" message:@"文件更新失败" data:@{}];
            completeHandle([JSBUtil objToJsonString:errDic],YES);
        }
        if (status == UnZipStatusSuccess) {//更新成功
            [[TipsHud sharedInstance] hideTips];
            NSLog(@"%@-----%@",downloadPath, unzipPath);
            NSMutableDictionary *result = [self setDict:@"200" message:@"更新成功" data:@{}];
            completeHandle([JSBUtil objToJsonString:result],YES);
        }
    }];
}

#pragma mark - 获取本地当前web信息
- (NSString *)getWebInfo:(NSString *)key {
    NSMutableDictionary *result = [self setDict:@"200" message:@"获取当前web页信息成功" data:@{
        /*appid*/
        @"appid" : @"",
        /*文件协议类型名*/
        @"agreement" : @"",
        /*app版本号*/
        @"appVersion" : AppVersion,
        /*当前本地web环境版本号*/
        @"webVersion" : @"1.0.0",
    }];
    for (UIView *view in self.zlzk_viewController.view.subviews) {
        if ([view isKindOfClass:[SXHBaseWebView class]]) {
            SXHBaseWebView *web = (SXHBaseWebView *)view;
            NSString *webVersion = [NSString stringWithFormat:@"%@+%@+%@",web.appid, web.agreement, AppVersion];
            result = [self setDict:@"200" message:@"获取当前web页信息成功" data:@{
                /*appid*/
                @"appid" : web.appid,
                /*本地文件协议名*/
                @"agreement" : web.agreement,
                /*app版本号*/
                @"appVersion" : AppVersion,
                /*当前本地web环境版本号*/
                @"webVersion" : kValueForKey(webVersion),
            }];
            return [JSBUtil objToJsonString:result];
        }
    }
    if ([self.zlzk_viewController isKindOfClass:[SXHWebViewController class]]) {
        SXHWebViewController *next = (SXHWebViewController *)self.zlzk_viewController;
        NSString *webVersion = [NSString stringWithFormat:@"%@+%@+%@",next.appid, next.agreement, AppVersion];
        result = [self setDict:@"200" message:@"获取当前web页信息成功" data:@{
            /*appid*/
            @"appid" : next.appid,
            /*本地文件协议名*/
            @"agreement" : next.agreement,
            /*app版本号*/
            @"appVersion" : AppVersion,
            /*当前本地web环境版本号*/
            @"webVersion" : kValueForKey(webVersion),
        }];
    }
    return [JSBUtil objToJsonString:result];
}

- (void)getWebInfo:(NSString *)key :(JSCallBack)completeHandle {
    NSString *result = [self getWebInfo:key];
    completeHandle(result,YES);
}

#pragma mark - 调起相机或者相册
///options obj = {saveNewImageAfterEdit:'编辑图片后是否保存'}
- (void)takePhoto:(id)options :(JSCallBack)completeHandle {
    NSString *saveNewImageAfterEdit = ((NSDictionary *)options)[@"saveNewImageAfterEdit"];
    [PhotoSelector shareInstance].saveNewImageAfterEdit = [saveNewImageAfterEdit boolValue];
    [[PhotoSelector shareInstance] openCamera:^(id  _Nullable result, NSMutableArray *errors) {
        NSArray *images = (NSArray *)result;
        NSMutableArray *tempArr = @[].mutableCopy;
        for (int i = 0; i < images.count; i++) {
            [tempArr addObject:[BaseTools imageToBase64:images[i]]];
        }
        NSMutableDictionary *resultDict = [self setDict:@"200" message:@"调起相机成功" data:@{
            @"images": tempArr
        }];
        completeHandle([JSBUtil objToJsonString:resultDict],YES);
    }];
}

///options obj = {maxSelectCount:'可选择相片数量最高9张',saveNewImageAfterEdit:'编辑图片后是否保存'}
- (void)getPhotos:(id)options :(JSCallBack)completeHandle {
    NSString *maxSelectCount = ((NSDictionary *)options)[@"maxSelectCount"];
    NSString *saveNewImageAfterEdit = ((NSDictionary *)options)[@"saveNewImageAfterEdit"];
    if (maxSelectCount == nil ||
        [maxSelectCount intValue] > 9 ||
        [maxSelectCount intValue] < 1) {
        maxSelectCount = @"9";
    }
    [PhotoSelector shareInstance].maxSelectCount = [maxSelectCount intValue];
    [PhotoSelector shareInstance].saveNewImageAfterEdit = [saveNewImageAfterEdit boolValue];
    [[PhotoSelector shareInstance] openPhotos:^(id  _Nullable result, NSMutableArray *errors) {
        NSArray *images = (NSArray *)result;
        NSMutableArray *tempArr = @[].mutableCopy;
        for (int i = 0; i < images.count; i++) {
            [tempArr addObject:[BaseTools imageToBase64:images[i]]];
        }
        NSMutableDictionary *resultDict = [self setDict:@"200" message:@"获取相册相片成功" data:@{
            @"images": tempArr
        }];
        completeHandle([JSBUtil objToJsonString:resultDict],YES);
    }];
}

#pragma mark - 调起二维码识别功能
//- (NSString *)recognitionCode:(NSString *)key;
- (void)recognitionCode:(NSString *)key :(JSCallBack)completeHandle {
//    QRCodeViewController *next = [[QRCodeViewController alloc] init];
    QRViewController *next = [[QRViewController alloc] init];
    next.modalPresentationStyle = UIModalPresentationFullScreen;
    WS(weakSelf)
    next.scanBlock = ^(NSString *result, RecognitionStatus recognitionStatus) {
        NSMutableDictionary *resultDict;
        if (recognitionStatus == RecognitionSuccess) {
            resultDict = [self setDict:@"200" message:@"识别成功" data:@{
                @"recognition": result
            }];
        }
        if (recognitionStatus == RecognitionFaield) {
            resultDict = [self setDict:@"500" message:@"识别失败" data:@{}];
        }
        if (recognitionStatus == RecognitionCancel) {
            resultDict = [self setDict:@"500" message:@"识别取消" data:@{}];
        }
        completeHandle([JSBUtil objToJsonString:resultDict],YES);
        [weakSelf.zlzk_viewController dismissViewControllerAnimated:YES completion:nil];
    };
    [self.zlzk_viewController presentViewController:next animated:YES completion:nil];
    
}

#pragma mark - 清除当前缓存
- (NSString *)cleanCache:(NSString *)key {
    if (!key || [key isEqualToString:@""]) {
        key = @"web";
    }
    if ([key isEqualToString:@"web"]) {
        
        [self deleteWebCache];
    }else if ([key isEqualToString:@"file"]) {
        
        [self clearTmpDirectory];
    }else { // all
        
        [self deleteWebCache];
        [self clearTmpDirectory];
    }
    NSMutableDictionary *resultDict = [self setDict:@"200" message:@"清除缓存成功" data:@{}];
    return [JSBUtil objToJsonString:resultDict];
}

- (void)cleanCache:(NSString *)key :(JSCallBack)completeHandle {
    NSString *result = [self cleanCache:key];
    completeHandle(result,YES);
}

#pragma mark - private methods

- (void)deleteWebCache {
    //allWebsiteDataTypes清除所有缓存
    NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
    NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        
    }];
//    NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
//    NSUserDomainMask, YES)[0];
//    NSString *bundleId  =  [[[NSBundle mainBundle] infoDictionary]
//    objectForKey:@"CFBundleIdentifier"];
//    NSString *webkitFolderInLib = [NSString stringWithFormat:@"%@/WebKit",libraryDir];
//    NSString *webKitFolderInCaches = [NSString
//    stringWithFormat:@"%@/Caches/%@/WebKit",libraryDir,bundleId];
//    NSString *webKitFolderInCachesfs = [NSString
//     stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId];

//    NSError *error;
//    /* iOS8.0 WebView Cache的存放路径 */
//    [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCaches error:&error];
//    [[NSFileManager defaultManager] removeItemAtPath:webkitFolderInLib error:nil];
//    /* iOS7.0 WebView Cache的存放路径 */
//    [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCachesfs error:&error];
}

- (void)clearTmpDirectory {
    NSArray *tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:nil];
    for (NSString *file in tmpDirectory) {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:nil];
    }
}

#pragma mark - 跳转web页
- (NSString *)jumpFrame:(id)options {
    if(![options isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *errDic = [self setDict:@"500" message:@"传入参类型错误" data:@{}];
        return [JSBUtil objToJsonString:errDic];
    }
    NSString *url = ((NSDictionary *)options)[@"url"];
    NSString *isHiddenNavigate = ((NSDictionary *)options)[@"isHiddenNavigate"];
    NSString *isHiddenTabbar = ((NSDictionary *)options)[@"isHiddenTabbar"];
    NSString *title = ((NSDictionary *)options)[@"title"];
    NSString *direction = ((NSDictionary *)options)[@"direction"];
    NSDictionary *dict = ((NSDictionary *)options)[@"params"];
    
    if ([self.zlzk_viewController isKindOfClass:[SXHWebViewController class]]) {
        
        self.zlzk_viewController.tabBarController.tabBar.hidden = [isHiddenTabbar boolValue];
        
        if ([direction isEqualToString:@"pop"]) { // 返回
            
            NSArray *viewControllers = self.zlzk_viewController.navigationController.viewControllers;
            
            if (self.zlzk_viewController.navigationController) {
                
                if ([viewControllers count] > 1) {
                    
                    id historyVC = [viewControllers objectAtIndex: viewControllers.count - 2];
                    
                    if ([historyVC isKindOfClass:[SXHWebViewController class]]) { //获取前一个页面控制器
                        
                        NSMutableDictionary *params = @{}.mutableCopy;
                        
                        [params addEntriesFromDictionary:((SXHWebViewController *)self.zlzk_viewController).params];
                        
                        [params addEntriesFromDictionary:dict];
                        
                        ((SXHWebViewController *)historyVC).hidesBottomBarWhenPushed = [isHiddenTabbar boolValue];
                        
                        ((SXHWebViewController *)historyVC).ishiddenNavigate = isHiddenNavigate;
                        
                        ((SXHWebViewController *)historyVC).params = params;
                        
                        if (![[NSString stringWithFormat:@"%@", ((SXHWebViewController *)historyVC).url] isEqualToString:[NSString stringWithFormat:@"%@", url]]) {
                            
                            ((SXHWebViewController *)historyVC).url = url;
                        }
                        
                        [self.zlzk_viewController.navigationController popToViewController:historyVC animated:YES];
                    }else {
                        
                        [self.zlzk_viewController.navigationController popViewControllerAnimated:YES];
                    }
                    
                }else {
                    
//                    [self.zlzk_viewController.navigationController popViewControllerAnimated:YES];
                }
            }else {
                
                [self.zlzk_viewController dismissViewControllerAnimated:YES completion:nil];
            }
        }else {
            NSString *className = [NSString stringWithUTF8String:object_getClassName(self.zlzk_viewController)];
            Class class = NSClassFromString(className);
            SXHWebViewController *next = [class new];
//            SXHWebViewController *next = [SXHWebViewController new];
            NSMutableDictionary *params = @{}.mutableCopy;
            [params addEntriesFromDictionary:((SXHWebViewController *)self.zlzk_viewController).params];
            [params addEntriesFromDictionary:dict];
            next.params = params;
            next.url = url;
            next.hidesBottomBarWhenPushed = YES;
            next.ishiddenNavigate = isHiddenNavigate;
            next.title = title;
                
            if (self.zlzk_viewController.navigationController) {

                [self.zlzk_viewController.navigationController pushViewController:next animated:YES];
            }else {
                
                next.modalPresentationStyle = UIModalPresentationFullScreen;
                [self.zlzk_viewController presentViewController:next animated:YES completion:nil];
            }
        }
        
    }
    NSMutableDictionary *result = [self setDict:@"200" message:@"打开下的web页成功" data:@{}];
    return [JSBUtil objToJsonString:result];
}

- (void)jumpFrame:(id)options :(JSCallBack)completeHandle {
    NSString *result = [self jumpFrame:options];
    completeHandle(result,YES);
}

#pragma mark - 地图定位
//- (NSString *)location:(id)options;
- (void)location:(id)options :(JSCallBack)completeHandle {
    if(![options isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *errDic = [self setDict:@"500" message:@"传入参类型错误" data:@{}];
        completeHandle([JSBUtil objToJsonString:errDic], YES);
    }
    NSString *showMap = ((NSDictionary *)options)[@"showMap"];
    NSString *title = ((NSDictionary *)options)[@"title"];
    NSMutableArray *locationArr = @[
                            @{@"latitude":@"",@"longitude":@"",@"address":@"",@"mapType":@"GCJ-02"}.mutableCopy,
                            @{@"latitude":@"",@"longitude":@"",@"address":@"",@"mapType":@"BD09"}.mutableCopy
                            ].mutableCopy;
    
    if ([showMap boolValue]) { // 展示地图
        SXHMapViewController *mapview = [[SXHMapViewController alloc] initWithTitle:title complete:^(CLLocation * _Nullable location, NSString * _Nullable address, NSError *error) {
                                         
            if (error == nil) {
                //坐标转换
                CLLocationCoordinate2D GCJ02 = [LocationConverter wgs84ToGcj02:location.coordinate];
                CLLocationCoordinate2D BD09 = [LocationConverter wgs84ToBd09:location.coordinate];
                
                for (NSMutableDictionary *temp in locationArr) {
                    if ([(NSString *)temp[@"mapType"] isEqualToString:@"BD09"]) {
                        temp[@"latitude"] = [NSString stringWithFormat:@"%lf", BD09.latitude];
                        temp[@"longitude"] = [NSString stringWithFormat:@"%lf", BD09.longitude];
                        temp[@"address"] = address;
                    }
                    if ([(NSString *)temp[@"mapType"] isEqualToString:@"GCJ-02"]) {
                        temp[@"latitude"] = [NSString stringWithFormat:@"%lf", GCJ02.latitude];
                        temp[@"longitude"] = [NSString stringWithFormat:@"%lf", GCJ02.longitude];
                        temp[@"address"] = address;
                    }
                }
                NSMutableDictionary *result = [self setDict:@"200" message:@"获取地图定位成功" data:locationArr];
                completeHandle([JSBUtil objToJsonString:result], YES);
            }else {
                NSMutableDictionary *result = [self setDict:@"500" message:error.userInfo[@"NSLocalizedDescription"] data:@{}];
                completeHandle([JSBUtil objToJsonString:result], YES);
            }
        }];
        mapview.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.zlzk_viewController presentViewController:mapview animated:YES completion:nil];
        
    }else { // 不展示地图
        [[SXHLocationHandler shared] startLocation:^(CLLocation * _Nonnull location, NSString * _Nullable address) {
            //坐标转换
            CLLocationCoordinate2D GCJ02 = [LocationConverter wgs84ToGcj02:location.coordinate];
            CLLocationCoordinate2D BD09 = [LocationConverter wgs84ToBd09:location.coordinate];
            for (NSMutableDictionary *temp in locationArr) {
                if ([(NSString *)temp[@"mapType"] isEqualToString:@"BD09"]) {
                    temp[@"latitude"] = [NSString stringWithFormat:@"%lf", BD09.latitude];
                    temp[@"longitude"] = [NSString stringWithFormat:@"%lf", BD09.longitude];
                    temp[@"address"] = address;
                }
                if ([(NSString *)temp[@"mapType"] isEqualToString:@"GCJ-02"]) {
                    temp[@"latitude"] = [NSString stringWithFormat:@"%lf", GCJ02.latitude];
                    temp[@"longitude"] = [NSString stringWithFormat:@"%lf", GCJ02.longitude];
                    temp[@"address"] = address;
                }
            }
            NSMutableDictionary *result = [self setDict:@"200" message:@"获取地图定位成功" data:locationArr];
            completeHandle([JSBUtil objToJsonString:result], YES);
        }];
    }
}


#pragma mark - setter
///处理返回数据
- (NSMutableDictionary *)setDict:(NSString *)code
                         message:(NSString *)message
                            data:(id)data{
    return @{
        @"code": code,
        @"message": message,
        @"data": data
    }.mutableCopy;
}
@end
